package com.infore.airsupervise.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.rey.material.app.BottomSheetDialog;
import com.vincent.filepicker.Constant;
import com.vincent.filepicker.activity.AudioPickActivity;
import com.vincent.filepicker.filter.entity.AudioFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;
import static com.vincent.filepicker.activity.AudioPickActivity.IS_NEED_RECORDER;

/**
 * @features: 描述+ 录音fragment
 * @author: WuTao
 * @date: 2017/5/6 16:04
 */
public class RecorderDiscribFragment extends BaseFragment {
    @BindView(R.id.recycler_recorder)
    RecyclerView recyclerRecorder;
    @BindView(R.id.et_mision_discrib)
    EditText etMisionDiscrib;

    private List<String> list_record = new ArrayList<>();
    private BaseQuickAdapter<String,BaseViewHolder> adapter;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recorder_selector, container, false);
        unbinder = ButterKnife.bind(this, view);


        initRecylceView();
        return view;
    }

    /**
     * 初始化录音列表
     */
    private void initRecylceView(){
        recyclerRecorder.setLayoutManager( new LinearLayoutManager(getContext()));
        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_selector,list_record) {
            @Override
            protected void convert(final BaseViewHolder helper, final String item) {
                String name = item.substring(item.lastIndexOf("/")+1);
                helper.setText(R.id.tv_title,name);
                //删除
                helper.getView(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        remove(helper.getLayoutPosition());
                    }
                });
                //预览选项
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent it = new Intent(Intent.ACTION_VIEW);
                        Uri uri = Uri.parse("file://"+item);
                        it.setDataAndType(uri,"audio/amr");
                        startActivity(it);
                    }
                });
            }
        };
        recyclerRecorder.setAdapter(adapter);
    }



    @OnClick(R.id.btn_recorder)
    public void onClick() {
        //打开录音界面
        Intent intent = new Intent(getContext(), AudioPickActivity.class);
        intent.putExtra(IS_NEED_RECORDER, true);
        intent.putExtra(Constant.MAX_NUMBER, 5);
        startActivityForResult(intent, Constant.REQUEST_CODE_PICK_AUDIO);
    }

    /**
     * 获取文件列表
     * @return
     */
    public List<String> getvoicePath(){
        return list_record;
    }

    /**
     * 获取描述信息
     * @return
     */
    public  String getDiscrib(){
        return etMisionDiscrib.getText().toString();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //录音
        if(resultCode == RESULT_OK && requestCode == Constant.REQUEST_CODE_PICK_AUDIO){
            ArrayList<AudioFile> listAudio = data.getParcelableArrayListExtra(Constant.RESULT_PICK_AUDIO);
            for ( AudioFile e : listAudio){
                list_record .add(e.getPath());
            }
        }
        adapter.notifyDataSetChanged();
    }
}
